package com.ltmonitor.web.controller;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.stat.TableStat.Mode;
import com.ltmonitor.adas.entity.AdasAlarm;
import com.ltmonitor.adas.entity.AdasAttachment;
import com.ltmonitor.entity.AlarmRecord;
import com.ltmonitor.entity.Department;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.vo.PaginateResult;
import com.ltmonitor.web.test.TestUser;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.OnlineUser;
import com.sun.tools.javadoc.Start;
import com.sun.xml.internal.rngom.digested.DPattern;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import sun.tools.tree.ThisExpression;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;


/**
 * 主动安全报警
 * 
 * @author shinion
 *
 */
@Controller
@RequestMapping("/activeSafeAlarm")
public class ActiveSafeAlarm extends PaginateAction {
	//定义mylog
	protected Logger mylog = Logger.getLogger(this.getClass());
	/**
	 * 
	 * 主动安全报警查询请求
	 * @param model
	 * @return 请求页面
	 */
	@RequestMapping("/view.action")
	public String view(Model model) {
		model.addAttribute("message", "sueecss");
		mylog.debug("前端请求主动安全报警页面");
		return "adasAlarm/activeSafeAlarm";
	}

	
	/**
	 * <p>请求adasAlarmList页面</p>
	 * <p>用户前端点击主动安全报警列表中单个报警记录</p>
	 * <p>发送查看该报警当天所有详细记录的请求</p>
	 * @param alarmIndex
	 * @return
	 */
	@RequestMapping("/viewAdasAlarmList.action")
	public String viewAdasAlarmList(@RequestParam(defaultValue = "") String alarmIndex, long id, Model model) {
		model.addAttribute("alarmIndex", alarmIndex);
		model.addAttribute("id", id);
		return "adasAlarm/viewAdasAlarmList";
	}
	
	
	
	/**
	 *<p>查看单条主动安全报警的页面</p>
	 * <p>用户前端点击主动安全报警信息列表中的查看链接</p>
	 * @param alarmIndex 请求参数alarmIndex
	 * @param id 请求参数id，为alarm表中的索引id
	 * @param model
	 * @return
	 */
	@RequestMapping("/oneAdasAlarmInfo.action")
	public String oneAdasAlarmInfo(@RequestParam(defaultValue = "") String alarmIndex, long id, Model model) {
		model.addAttribute("alarmIndex", alarmIndex);
		model.addAttribute("id", id);
		return "adasAlarm/oneAdasAlarmInfo";
	}
	
	
	/**
	 * 请求报警分级查询页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewAdasLevelInfoPage.action")
	public String viewAdasLevelInfo(Model model) {
		return "adasAlarm/viewAdasLevelInfoPage";
	}
	
	////////////////////////////////////////////////////////////////////////////////页面请求
////////////////////////////////////////////////////////////////////////////////
	
	
	/**
	 * 查询主表格：subiao.basicData
	 * 请求报警来源和报警类型基本数据：
	 * @return 返回包括报警来源和报警类型的值还有各自的描述，以链表的形式返回数据
	 */
	@RequestMapping("/getAlarmSources.action")
	public @ResponseBody List getAlarmSources() {
		List list = null;// 定义返回列表
		try {
			String querId = "activeSafeAlarm.selectAlarmSrouces";
			list = queryService.query(querId, null);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.error(e);
		}
		// 返回
		return list;
	}
	
	
	/**
	 * 查询主表格：subiao.basicData
	 * 请求查询报警处理描述数据
	 * @return 以链表形式返回数据
	 */
	@RequestMapping("/getAlarmProcessedRemarkData.action")
	public @ResponseBody List getAlarmProcessedRemarkData() {
		List list = null;
		try {
			try {
				String querId = "activeSafeAlarm.selectAlarmProcessedRemarkData";
				list = queryService.query(querId, null);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				log.error(e);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.error(e);
		}

		return list;
	}
	
	
	
	/**
	 * 查询主表格：gps_hisdata.alarm
	 * <p>对应页面：主动安全报警查询</p>
	 * <p>发送参数，请求查询gps_hisdata.alarm中主动安全报警信息</p>
	 * @param jsonStr 参数数据，以json的形式发送
	 * @return 分页数据
	 */
	@RequestMapping("/getAdasPageData.action")
	public @ResponseBody PaginateResult getAdasPageData(@RequestBody String jsonStr) {
		PaginateResult result = null;
		try {
			result = new PaginateResult();
			// 用于获取用户权限信息
			OnlineUser user = super.getOnlineUser();
			if (user != null) {
				Map<String, Object> map = JSONObject.fromObject(jsonStr);
				// 分页查询转换
				Integer pageNum = (Integer) map.get("pageNumber");
				Integer pageSize = (Integer) map.get("pageSize");
				if (pageNum == null)
					pageNum = 1;
				if (pageSize == null)
					pageSize = 15;
				
				//根据权限获取部门列表
				getBasicQueryData(user, map);
				
//				System.out.println("++++++++++++++");
//				
//				System.out.println("++++++++++++++");
//				
//				System.out.println(map);
//				
//				System.out.println("++++++++++++++");
//				
//				System.out.println("++++++++++++++");
				// 查询条件
				String queryId = "activeSafeAlarm.selectAdasAlarmPageDataFromAlarm";
				// 获取查询结果
				result = queryService.queryByPagination(queryId, map, pageNum, pageSize);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return result;
	}

	
	/**
	 * 查询主表格：gps_hisdata.alarm
	 * 对应页面：主动安全报警详细信息查询
	 * 通过alarmIndex获取获取一条adas报警数据的基本信息
	 * @param alarmIndex
	 * @return
	 */
	@RequestMapping("/getOneAdasAlarmFromAlarm.action")
	public @ResponseBody List getOneAdasAlarmFromAlarm(@RequestBody String jsonStr) {
		List result = null;
		try {
			Map<String, Object> map = JSONObject.fromObject(jsonStr);
			
			String queryId = "activeSafeAlarm.selectOneAdasAlarmFromAlarm";
			result = queryService.query(queryId, map);
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e);
			e.printStackTrace();
		}
		return result;
	}
	
	
	/**
	 * 查询主表格：subiaodb.adasalarm
	 * <P>对应页面：主动安全报警查询双击弹出窗口</P>
	 * <p>获取一条主动安全信息当前的所有该信息报警记录</p>
	 * <p>查询索引为alarmIndex</p>
	 * @param jsonStr 参数形式为json字符串
	 * @return 列表形式返回当前报警信息的所有详细信息
	 */
	@RequestMapping("/getAdasAlarmListData.action")
	public @ResponseBody List getAdasAlarmListData(@RequestBody String jsonStr) {
		List returnList = null;// 定义返回列表
		try {
			// 用于获取用户权限信息
			OnlineUser user = super.getOnlineUser();
			if (user != null) {
				Map<String, Object> map = JSONObject.fromObject(jsonStr);
				// 分页查询转换
				// Integer pageNum = (Integer) map.get("pageNumber");
				// Integer pageSize = (Integer) map.get("pageSize");
				// if(pageNum == null)
				// pageNum=1;
				// if(pageSize == null)
				// pageSize = 15;

				// System.out.println("+++++++++++++++++++++++");
				// System.out.println("+++++++++++++++++++++++");
				// System.out.println(map);
				// System.out.println("+++++++++++++++++++++++");
				// System.out.println("+++++++++++++++++++++++");

				String querId = "activeSafeAlarm.selectAdasAlarmListData";
				returnList = queryService.query(querId, map);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return returnList;
	}

	
	
	
	
	/**
	 *  <p>查询主表格：gps_hisdata.alarm</p>
	 * <p>对应页面：报警记录查询</p>
	 * <p>查询符合条件的报警记录</p>
	 * <p></p>
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/getAlarmProcessInfoPageData.action")
	public @ResponseBody PaginateResult getAlarmProcessInfoPageData(@RequestBody String jsonStr) {
		PaginateResult result = null;
		try {
			result = new PaginateResult();
			// 用于获取用户权限信息
			OnlineUser user = super.getOnlineUser();
			if (user != null) {
				Map<String, Object> map = JSONObject.fromObject(jsonStr);
				// 分页查询转换
				Integer pageNum = (Integer) map.get("pageNumber");
				Integer pageSize = (Integer) map.get("pageSize");
				if (pageNum == null)
					pageNum = 1;
				if (pageSize == null)
					pageSize = 15;

				//获取查询部门编号
				getBasicQueryData(user, map);
				
				 System.out.println("++++++++++++++++++++++++++++++++");
				 System.out.println("++++++++++++++++++++++++++++++++");
				 System.out.println(map);
				 System.out.println("++++++++++++++++++++++++++++++++");
				 System.out.println("++++++++++++++++++++++++++++++++");
				
				// 查询条件
				String queryId = "activeSafeAlarm.selectAlarmProcessInfoPageData";
				// 获取查询结果
				result = queryService.queryByPagination(queryId, map, pageNum, pageSize);
				// System.out.println("++++++++++++++++++++++++++++++++");
				// System.out.println("++++++++++++++++++++++++++++++++");
				// System.out.println(result);
				// System.out.println("++++++++++++++++++++++++++++++++");
				// System.out.println("++++++++++++++++++++++++++++++++");

			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return result;
	}

	
	
	
	/**
	 * <p>查询主表格：：gps_hisdata.alarm</p>
	 * <p>对应页面：主页面报警数据</p>
	 * <p>查询所有未处理过的数据</p>
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/getAlarmUnprocessedPageData.action")
	public @ResponseBody PaginateResult getAlarmUnprocessedPageData(@RequestBody String jsonStr) {
		PaginateResult result = null;
		try {
			result = new PaginateResult();
			// 用于获取用户权限信息
			OnlineUser user = super.getOnlineUser();
			if (user != null) {
				Map<String, Object> map = JSONObject.fromObject(jsonStr);
				// 分页查询转换
				Integer pageNum = (Integer) map.get("pageNumber");
				Integer pageSize = (Integer) map.get("pageSize");
				if (pageNum == null)
					pageNum = 1;
				if (pageSize == null)
					pageSize = 15;

				//获取查询部门编号
				getBasicQueryData(user, map);
				
				
				
				// 查询条件
				String queryId = "activeSafeAlarm.selectAlarmUnprocessedPageData";
				// 获取查询结果
				result = queryService.queryByPagination(queryId, map, pageNum, pageSize);
				// System.out.println("++++++++++++++++++++++++++++++++");
				// System.out.println("++++++++++++++++++++++++++++++++");
				// System.out.println(result);
				// System.out.println("++++++++++++++++++++++++++++++++");
				// System.out.println("++++++++++++++++++++++++++++++++");
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return result;
	}
	
	
	
	
	/**
	 * <p>根据用户基本信息获取部门查询条件</p>
	 * <p>首先根据部门编号depId获取部门链表</p>
	 * <p>如果部门编号depId为空，则通过userId查询用户权限下所有部门</p>
	 * <p>把获取的数据放入map中，key为"depIdList"</p>
	 * @param user 当前用户
	 * @param map 查询条件集合
	 */
	private void getBasicQueryData(OnlineUser user, Map map) {
		if (user != null) {
			// 获取用户id
			long userId = user.getEntityId();
			map.put("userId", userId);
			// 根据用户id获取部门列表
			Integer depId = (Integer) map.get("depId");
			if (depId == null)
				depId = 0;

			if (depId > 0) {
				List<Long> depIdList = super.getDepartmentService().getDepIdList(depId);
				map.put("depIdList", depIdList);
				map.remove("depId");
			} else {
				// 如果部门编号为空，查询所有，那么通过userId获取部门编号
				if (userId > 0) {
					List<Department> list = super.getDepartmentService().getDepartmentListByUserId(userId);
					if (list.size() > 0) {
						List<Long> depIdList2 = new ArrayList<Long>();
						for (Department temp : list) {
							depIdList2.add(temp.getEntityId());
						}
						map.put("depIdList", depIdList2);
						map.remove("depId");
					}
				}
			} // end depId
		} // end user
	}	
	


	
	/**
	 * 获取查询结果，把查询封装为一个接口，输入查询语句和参数，自动输出结果
	 * @param queryId 查询语句
	 * @param jsonStr 查询参数 ，json格式
	 * @return
	 */
	private JsonMessage getResultDatas(String queryId,String jsonStr) {
		JsonMessage result = null;
		PaginateResult dataList = new PaginateResult();
		try {
			// 用于获取用户权限信息
			OnlineUser user = super.getOnlineUser();
			if (user != null) {
				// 解析上传数据
				Map<String, Object> map = JSONObject.fromObject(jsonStr);
				// 分页查询转换
				Integer pageNum = (Integer) map.get("pageNumber");
				Integer pageSize = (Integer) map.get("pageSize");
				if (pageNum == null)
					pageNum = 1;
				if (pageSize == null)
					pageSize = 15;

				// 获取权限获取基本的查询条件
				getBasicQueryData(user, map);

				// 获取查询结果
				dataList = queryService.queryByPagination(queryId, map, pageNum, pageSize);
				
			}
			result = new JsonMessage(true, dataList);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = new JsonMessage(false, e.getMessage());
		}
		return result;
	}
	

	

	
	/**
	 * 请求主动安全报警附件查询页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewAdasAttachment.action")
	public String viewAdasAttachment(Model model) {
		model.addAttribute("message", "sueecss");
		return "adasAlarm/viewAdasAttachment";
	}

	/**
	 * 获取主动安全报警附件数据信息，并根据请求分页数据筛选后返回
	 * 
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/getAdasAttachmentPageData.action")
	public @ResponseBody PaginateResult getAdasAttachmentPageData(@RequestBody String jsonStr) {
		PaginateResult result = null;
		try {
			result = new PaginateResult();
			// 用于获取用户权限信息
			OnlineUser user = super.getOnlineUser();
			if (user != null) {
				// 解析上传数据
				Map<String, Object> map = JSONObject.fromObject(jsonStr);
				// 分页查询转换
				Integer pageNum = (Integer) map.get("pageNumber");
				Integer pageSize = (Integer) map.get("pageSize");
				if (pageNum == null)
					pageNum = 1;
				if (pageSize == null)
					pageSize = 15;

				// 获取权限获取基本的查询条件
				getBasicQueryData(user, map);

				// 查询语句
				String queryId = "activeSafeAlarm.selectAdasAttachmentPageData";
				// 获取查询结果
				result = queryService.queryByPagination(queryId, map, pageNum, pageSize);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	
	
	
	
	/**
	 * 请求报警明细查询页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewAllAlarms.action")
	public String viewAllAlarms(Model model) {
		model.addAttribute("message", "success");
		return "alarm/viewAllAlarms";
	}
	
	@RequestMapping("/getAllAlarmsPageDatas.action")
	public @ResponseBody JsonMessage getAllAlarmsPageDatas( @RequestBody String jsonStr) {
		String queryId = "activeSafeAlarm.selectAllAlarmsPageDatas";
		return getResultDatas(queryId, jsonStr);
	}
	
	
	
	
/**
 * 请求报警次数统计页面
 * @param model
 * @return
 */
@RequestMapping("/viewCountAlarmsNums.action")
	public String viewCountAlarmsNums(Model model) {
	model.addAttribute("message", "success");
		return "alarm/viewCountAlarmsNums";
	}
	

@RequestMapping("/getCountAlarmsNumsPagedatas.action")
public @ResponseBody JsonMessage getCountAlarmsNumsPagedatas(@RequestBody String jsonStr) {
	String queryId = "activeSafeAlarm.selectCountAlarmsNumsPagedatas";
	return getResultDatas(queryId, jsonStr);
}
	





	
	
	
	/*
	 * 以上为保留方法，不需要再次修改
	 * 
	 * 
	 * 以下方法为暂时使用，需要修改确认
	 */
	
	
	
	
	
	
	

	

	
	
	/**
	 *
	 * 获取主动安全报警附件数据，测试方法
	 * 
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/getAdasAlarmByPlateNoFromAdasAlarm.action")
	public @ResponseBody JsonMessage getAdasAlarmByPlateNoFromAdasAlarm(@RequestBody String jsonStr) {
		JsonMessage result = null;
		try {
			List data = null;
			data = new ArrayList<Object>();
			// 解析上传数据
			Map<String, Object> map = JSONObject.fromObject(jsonStr);
			// 首先查询总数
			String queryId = "activeSafeAlarm.selectAdasAlarmByPlateNoFromAdasAlarm";
			List listTemp = queryService.query(queryId, map);
			data.add(listTemp);
			
			queryId = "alarm.selectNewAlarms";
			listTemp = queryService.query(queryId, map);
			data.add(listTemp);
			
			result = new JsonMessage(true, data);
			System.out.println("+++++++++++++++++++++++");
			System.out.println("+++++++++++++++++++++++");
			System.out.println("+++++++++++++++++++++++");
			System.out.println(result.toString());
			System.out.println("+++++++++++++++++++++++");
			System.out.println("+++++++++++++++++++++++");
			System.out.println("+++++++++++++++++++++++");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = new JsonMessage(false, e.getMessage());
		}
		return result;
	}

				

}
